package mysql

import (
	"gorm.io/gorm"
)

type Course struct {
	gorm.Model
	Name   string  `gorm:"type:varchar(20);not null;comment:课程名;index"`
	Stock  int     `gorm:"type:int(5);not null;comment:数量"`
	Price  float64 `gorm:"type:decimal(10,2);not null;comment:课程价格"`
	Cover  string  `gorm:"type:varchar(255);not null;comment:封面"`
	Intro  string  `gorm:"type:varchar(35);not null;comment:描述"`
	Status int     `gorm:"type:int(1);not null;default:1;comment:1-售卖中 2-已售空"`
}

func (c *Course) Add() error {
	return DB.Create(&c).Error
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
